package main

import (

	"fmt"
	_ "github.com/denisenkom/go-mssqldb"
	. "ircove.com/irc"
	"ircove.com/services"
	"log"
	"net"
	"time"
	"ircove.com/repository"
)

type Options struct {
	Network string
	Address string
}

func Getint(buf []byte,order bool)  {



}
func main() {

	repository.RegisterLua()
	//db := repository.RedisConnection()
	//db.Set("123","134",time.Second*60)
	//cmd:= db.Get("123")
	//
	//println(cmd.Val())
	//repository.LuaScript("UserAuth").Run(db,[]string{
    	//"1111","2222222222",1232323
	//})



	opts := &ServerOptions{
		Network:     "tcp4",
		Address:     "192.168.4.248:4040",
		MaxConnSize: 100,
		TimeOut:     65,
	}
	services.Reg()
	server := new(Server)
	server.Opts = opts
	server.ServerName="gopush"
    server.Clients=make(map[string]*Client,4096)

	go server.Start()
	conn, err := net.Dial(server.Opts.Network, server.Opts.Address)
	if err != nil {
		log.Println(err)
	}

	go func() {




		for {
			message, err := NewMessage(MessageType_HeartBeat, MessageFormat_JSON, 0, nil)
			if err != nil {
				fmt.Println(err)
			}
			err = message.Encode(conn)
			if err != nil {
				fmt.Println(err)
			}

			time.Sleep(time.Second)
		}


	}()

	go func() {

		for {

			message1 := &Message{}
			err = message1.Decode(conn)
			if err != nil {
				fmt.Println(err)
			}
			respons := string(message1.Data)
			fmt.Println(respons)
		}
	}()

	exit:=make(chan struct{})

	select {
	   case <-exit:
	   	println("exit")

	}
	//fmt.Print(time.Now().Unix())
	//fmt.Print(common.VFCode_Success)
	//header,err :=irc.NewHeader(irc.MessageType_Connect,irc.MessageFormat_Json,256)
	//if err !=nil {
	//	fmt.Print(err.Error())
	//}
	//
	//fmt.Println(header.MessageType)
	//fmt.Println(header.MessageFormat)
	//fmt.Println(header.MsgLen)
	//fmt.Println(header.LengthFlag)
	//buf:=&bytes.Buffer{}
	//if err= header.Encode(buf,256);err != nil{
	//  fmt.Println(err.Error())
	//}
	//fmt.Printf("len :%d",len(buf.Bytes()))
	//
	//
	//
	//fmt.Println(header.MessageType)
	//fmt.Println(header.MessageFormat)
	//fmt.Println(header.MsgLen)
	//fmt.Println(header.LengthFlag)

	//chs :=make([]chan int,10)
	//for i:=0;i<10 ;i++  {
	//	chs[i]=make(chan int)
	//	go count(chs[i])
	//}
	//
	//
	//for _ ,ch :=range chs {
	//
	//	 i:= <-ch
	//
	//	 fmt.Println(i)
	//}
	//var network string= "tcp"
	//var address string=":8080"
	//
	//ip_addr, err:= net.ResolveTCPAddr(network,address);
	//if err!=nil{
	//	log.Fatalf("tcpaddress %s error ! %s",address,err.Error())
	//}
	//ln, err := net.ListenTCP(network,ip_addr)
	//if err != nil {
	//	log.Fatalf("listen %s error ! %s",address,err.Error())
	//}
	//for {
	//	conn, err := ln.Accept()
	//	if err != nil {
	//		log.Fatalf("Accept %s error ! %s",address,err.Error())
	//	}
	//	go handleConnection(conn)
	//}
}

func handleConnection(conn net.Conn) {
	defer conn.Close()
}
